<?php
/* 
 *
 */
class QinRegion
{
  private $db;
  public function __construct()
  {
    $this->db = QinDb::instance();
    $this->db->exec('set names utf8;');
    Zend_Db_Table::setDefaultAdapter($this->db);
  }

  public function getProvinceList()
  {
    return $this->getRegion("code like '%0000'");
  }

  public function getCitiesList($province_id)
  {
    $head = substr($province_id, 0, 2);
    $city_code = $head . "%00"; 
    return $this->getRegion("code like '$city_code' and code != '$province_id'");
  }
  public function getTownsList($city_id)
  {
    $head = substr($city_id, 0, 4);
    $city_code = $head . "%"; 
    return $this->getRegion("code like '$city_code' and code != '$city_id'");
  }
    
  public function getRegion($where)
  {
    $mapper = new Application_Model_RegionMapper();
    $res = $mapper->fetchAll($where);
    $array = array();
    foreach($res as $region)
      {
	$array[$region->get('code')] = $region->get('name');
      }
    return $array;
  }
};
